<?php
// No direct access
defined('_JEXEC') or die;

$dbPrefix = $this->component->dbprefix;
$tables = GenCodeHelper::getTables();
?>

<style>
<!--
li.join-table select {
	width: 140px;
}

li.join-table select.width-70 {
	width: 70px;
}

li.join-table select.width-110 {
	width: 110px;
}

li span, li small {
	float: left;
	line-height: 25px;
	margin-right: 5px;
}

span.fields-container {
	margin: 0;
}

li#tmpl-join-fields {
	display: none;
}

li.filter span {
	/* margin-right: 20px; */
}

-->
</style>

<div id="default-list">
	<ul id="select-table-container">
		<li id="list-model-forms">
			<label>Model Form</label>
			<select id="model_form_id" name="jform[model_form_id]">
				<option value="">- Select -</option>
				<?php foreach ($this->modelForms as $model): ?>
				<?php $selected = ($model->id == $this->item->model_form_id) ? 'selected="selected"' : null; ?>
				<option value="<?php echo $model->id; ?>" <?php echo $selected; ?>><?php echo $model->name; ?></option>
				<?php endforeach; ?>
			</select>
		</li>
		<li id="list-select-table">
			<label>Select Table</label>
			<select id="table_id" name="jform[table_id]">
				<option value="">- Select -</option>
				<?php foreach ($this->tables as $table): ?>
				<?php $selected = ($table->id == $this->item->table_id) ? 'selected="selected"' : null; ?>
				<option value="<?php echo $table->id; ?>" <?php echo $selected; ?>><?php echo $dbPrefix . $table->name; ?></option>
				<?php endforeach; ?>
			</select>
		</li>
		<li>
			<label>&nbsp;</label>
			<div id="list-table-fields" style="float: left; width: 830px; border: 1px solid #CCC; padding: 5px;">
				Select table
			</div>
		</li>
		<?php 
		$filter = unserialize($this->item->fields_to_filter);
		
		$checkState = isset($filter['state']) ? 'checked="checked"' : null;
		$checkCategory = isset($filter['category']) ? 'checked="checked"' : null;
		$checkCreatedBy = isset($filter['created_by']) ? 'checked="checked"' : null;
		$checkLanguage = isset($filter['language']) ? 'checked="checked"' : null;
		$catValue = isset($filter['catid']) ? $filter['catid'] : null;
		$languageValue = isset($filter['language_id']) ? $filter['language_id'] : null;
		?>
		<li class="filter">
			<label>Filter by:</label>
			<input type="checkbox" name="jform[fields_to_filter][state]" value="1" <?php echo $checkState; ?> /> <span>State</span>
			<input type="checkbox" name="jform[fields_to_filter][created_by]" value="1" <?php echo $checkCreatedBy; ?> /> <span>Created by</span>
			<input type="checkbox" name="jform[fields_to_filter][category]" value="1" <?php echo $checkCategory; ?> /> <span>Category (Field ID:</span>
			<input type="text" size="10" name="jform[fields_to_filter][catid]" title="Field category id in table" value="<?php echo $catValue; ?>" />
			<span>)</span>
			<input type="checkbox" name="jform[fields_to_filter][language]" value="1" <?php echo $checkLanguage; ?> /> <span>Language (Field ID:</span>
			<input type="text" size="10" name="jform[fields_to_filter][language_id]" title="Field language id in table" value="<?php echo $languageValue; ?>" />
			<span>)</span>
		</li>
		<li>
			<label>Fields to search:</label>
			<input 
					name="jform[fields_to_search]" 
					id="jform_fields_to_search"
					value="<?php echo $this->item->fields_to_search;?>" 
					size="60" />
		</li>
		<li>
			<label>&nbsp;</label>
			<small>
				List fields, seperate by comma. Example: id, title. 
				If you want to search by LIKE, add star before field. 
				Example: id, *title
			</small>
		</li>
		<li>
			<label>&nbsp;</label>
			<button type="button" id="btn-join-table">Add table to join</button>
		</li>
		
		<?php 
		$joins = $this->joins;
		
		if(!empty($joins)):
		foreach ($joins as $key => $join):
		?>
		<li class="join-table">
			<label>&nbsp;</label>
			<select name="jform[join_type][]" class="width-70">
				<?php 
				$selectedInner = ($join->join_type == 'INNER') ? 'selected="selected"' : null;
				$selectedLeft = ($join->join_type == 'LEFT') ? 'selected="selected"' : null;
				$selectedRight = ($join->join_type == 'RIGHT') ? 'selected="selected"' : null;
				?>
				<option value="INNER" <?php echo $selectedInner; ?>>INNER</option>
				<option value="LEFT" <?php echo $selectedLeft; ?>>LEFT</option>
				<option value="RIGHT" <?php echo $selectedRight; ?>>RIGHT</option>
			</select>
			<span>JOIN</span>
			<select name="jform[join_table][]" class="select-join-table-1" id="join_table_on_1_<?php echo $key; ?>">
				<option value="">- Table -</option>
				<?php foreach ($tables AS $tableKey => $tableValue): ?>
				<?php $selectedTable = ($join->join_table == $tableKey)  ? 'selected="selected"' : null; ?>
				<option value="<?php echo $tableKey; ?>" <?php echo $selectedTable; ?>><?php echo $tableValue; ?></option>
				<?php endforeach; ?>
			</select>
			<span>[ Fields: </span>
			<input 
				title="Fields in join table. Example: id, title" 
				id="join_table_fields_<?php echo $key; ?>" 
				name="jform[join_table_fields][]" 
				value="<?php echo $join->join_table_fields; ?>" />
			<span>] ON</span>
			<span class="fields-container">
				<select class="select-join-table-fields-1 width-110" name="jform[join_field_on_1][]">
					<option value="-">- Field -</option>
				</select>
			</span>
			<span>=</span>
			<select class="select-join-table-2" id="join_table_on_2_<?php echo $key; ?>" name="jform[join_table_on_2][]">
				<option value="">- Table -</option>
				<?php foreach ($tables AS $tableKey => $tableValue): ?>
				<?php $selectedTable = ($join->join_table_on_2 == $tableKey)  ? 'selected="selected"' : null; ?>
				<option value="<?php echo $tableKey; ?>" <?php echo $selectedTable; ?>><?php echo $tableValue; ?></option>
				<?php endforeach; ?>
			</select>
			<span>.</span>
			<span class="fields-container">
				<select class="select-join-table-fields-2 width-110" name="jform[join_field_on_2][]">
					<option value="-">- Field -</option>
				</select>
			</span>
			<a class="del-join-table" href="#del-join-table" style="float: left; line-height: 25px;">[ del ]</a>
		</li>
		<?php 
		endforeach;
		endif;
		?>
		
		<li class="join-table" id="tmpl-join-fields">
			<label>&nbsp;</label>
			<select name="jform[join_type][]" class="width-70">
				<option value="INNER">INNER</option>
				<option value="LEFT">LEFT</option>
				<option value="RIGHT">RIGHT</option>
			</select>
			<span>JOIN</span>
			<select name="jform[join_table][]" class="select-join-table-1">
				<option value="">- Table -</option>
				<?php foreach ($tables AS $tableKey => $tableValue): ?>
				<option value="<?php echo $tableKey; ?>"><?php echo $tableValue; ?></option>
				<?php endforeach; ?>
			</select>
			<span>[ Fields: </span>
			<input 
				title="Fields in join table. Example: id, title" 
				name="jform[join_table_fields][]" />
			<span>] ON</span>
			<span class="fields-container">
				<select class="select-join-table-fields-1 width-110" name="jform[join_field_on_1][]">
					<option value="-">- Field -</option>
				</select>
			</span>
			<span>=</span>
			<select class="select-join-table-2" name="jform[join_table_on_2][]">
				<option value="">- Table -</option>
				<?php foreach ($tables AS $tableKey => $tableValue): ?>
				<option value="<?php echo $tableKey; ?>"><?php echo $tableValue; ?></option>
				<?php endforeach; ?>
			</select>
			<span>.</span>
			<span class="fields-container">
				<select class="select-join-table-fields-2 width-110" name="jform[join_field_on_2][]">
					<option value="-">- Field -</option>
				</select>
			</span>
			<a class="del-join-table" href="#del-join-table" style="float: left; line-height: 25px;">[ del ]</a>
		</li>
	</ul>
</div>

<input type="hidden" name="edit_mode" value="list" />